import {Message} from 'tinper-bee';
import jdiwork from './jdiwork'
// 默认toast展示样式
let vendorsInfo = function (param) {
    let defaultText = param.text || '不允许输入重复文档名称';
    Message.create({content: defaultText, duration: 3, color: 'info'});
};
// 错误toast展示样式
let vendorsDanger = function (param) {
    Message.destroy();
    let defaultText = param.text || '不允许输入重复文档名称';
    Message.create({content: defaultText, duration: 99999999, color: 'danger'});
};
// 成功toast展示样式
let vendorsSuccess = function (param) {
    Message.destroy();
    let defaultText = param.text || '不允许输入重复文档名称';
    Message.create({content: defaultText, duration: 3, color: 'success'});
};
// 成功toast时间为5
let vendorsLongSuccess = function (param) {
    Message.destroy();
    let defaultText = param.text;
    Message.create({content: defaultText, duration: 5, color: 'success'});
};
// 警告toast展示样式
let vendorsWarning = function (param) {
    let defaultText = param.text || '不允许输入重复文档名称';
    Message.create({content: defaultText, duration: 3, color: 'warning'});
};
// 数字补零
let vendorsToDubble = function (num) {
    return String(num).length == 1 ? '0' + num : num;
}
// 格式化日期
let vendorsFmtDate = function (obj) {
    if (obj === null || obj === undefined) return ''
    var date = new Date(obj);
    var y = date.getFullYear();
    var m = vendorsToDubble(date.getMonth() + 1);
    var d = vendorsToDubble(date.getDate());
    return y + "-" + m + "-" + d;
}
// 获取url参数
let vendorsGetUrlParam = function (url) {
    let param = {};
    let reg = /([^?&=#]+)=([^?&=#]+)/g;
    url.replace(reg, (a, b, c) => {
        param[b] = c;
    });
    return param;
};
//拼接url
let vendorsSetUrl = (url, params) => {
    url = `${url}?`;
    for (let key in params) {
        url += `${key}=${params[key]}&`
    }
    url = url.substring(0, url.length - 1);
    return url
}
//创建sessionStorage  进行页面传值  value为对象数据类型
let vendorsSetStorage = (key, value) => {
    if (Object.prototype.toString.call(value) !== '[object Object]') {
        throw 'storage value must be Object';
    }
    sessionStorage.setItem(key, JSON.stringify(value));
};
//获取sessionStorage
let vendorsGetStorage = (key) => {
    let value = sessionStorage.getItem(key);
    return typeof value === 'String' ? value : JSON.parse(value)
};
//根据key删除sessionStorage中的一个
let vendorsRemoveStorage = (key) => {
    sessionStorage.removeItem(key);
};
// 格式化字符串前后空格
let vendorsTrimStr = function (str) {
    return str.replace(/^\s+|\s+$/g, "");
};
// 格式化字符串全部
let vendorsRemoveAllSpace = function (str) {
    return str.replace(/\s+/g, "");
};
// test ie
let vendorsIEVersion = function () {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {
            return 7;
        } else if (fIEVersion == 8) {
            return 8;
        } else if (fIEVersion == 9) {
            return 9;
        } else if (fIEVersion == 10) {
            return 10;
        } else {
            return 6;//IE版本<=7
        }
    } else if (isEdge) {
        return 'edge';//edge
    } else if (isIE11) {
        return 11; //IE11  
    } else {
        return -1;//不是ie浏览器
    }
}

let vendorsSetDateLoop = (data) => {
    let minStartDate = null, maxEndDate = null;
    data.children.forEach((item, index) => {
        if (item.children) {
            vendorsSetDateLoop(item);
        }
        if (minStartDate === null || item.planStartDate < minStartDate) {
            minStartDate = item.planStartDate;
        }
        if (maxEndDate === null || item.planEndDate > maxEndDate) {
            maxEndDate = item.planEndDate;
        }
    })
    if (minStartDate !== null && maxEndDate !== null) {
        data.planStartDate = minStartDate;
        data.planEndDate = maxEndDate;
        data.planDays = Math.round((new Date(data.planEndDate).getTime() - new Date(data.planStartDate).getTime()) / 1000 / 60 / 60 / 24) + 1;
    }
}
let vendorsGetTenantId = () =>{
    let tenantId;
    if(document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1)){
        tenantId = document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1).split('=')[1];
    }else {
        jdiwork().getContext((data) => {
            tenantId = data.tenantid
        });
    }
    return tenantId
}

function getJdiworkTenantId(){
    return new Promise((reject)=>{
        jdiwork().getContext((data) => {
            reject(data.tenantid)
        });
    })
}
let vendorsGetTId = async () =>{
    let tenantId;
    if(document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1)){
        tenantId = document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1).split('=')[1];
    }else {
        tenantId = await getJdiworkTenantId()
    }
    return tenantId
}
export {
    vendorsInfo,
    vendorsFmtDate,
    vendorsDanger,
    vendorsSuccess,
    vendorsGetTId,
    vendorsLongSuccess,
    vendorsWarning,
    vendorsToDubble,
    vendorsGetUrlParam,
    vendorsTrimStr,
    vendorsRemoveAllSpace,
    vendorsIEVersion,
    vendorsSetDateLoop,
    vendorsSetUrl,
    vendorsSetStorage,
    vendorsGetStorage,
    vendorsRemoveStorage,
    vendorsGetTenantId
}
